<template>
  <customTree :data="treeData" @click="click"/>
</template>

<script>

export default {
  desc: '用户菜单权限树',
  name: 'treeMenuUser',
  components: {
  },
  props: {
    /**
     * @Description：树容器最大高度减去值
     * @Date 2023-10-10
     */
    userId: {
      type: String,
      default: () => -1,
    },
    /**
     * @Description：树容器最大高度减去值
     * @Date 2023-10-10
     */
    subHeight: {
      type: Number,
      default: () => 160,
    }
  },
  created() {
  },
  data() {
    return {
      treeData: []
    }
  },
  methods: {
    // 加载数据
    load() {
      this.$ajax.get('/menu/listByUser', {
        params: {
          userId: this.userId
        }
      }).then(response => {
        if (response.code == 0) {
          this.treeData = global.utils.convertToTree(response.data, {
            rootNode: {id: 0, parentId: 'none', title: '所有菜单'},
            keyFieldName: 'id',
            parentFieldName: 'parentId',
            labelFieldName: 'title'
          }) // {CHECK_HERE}
        } else {
          this.utilMessage.error(response.msg)
        }
      })
    },

    // 节点点击时触发
    click(nodeData) {
      this.$emit('click', nodeData)
    }
  },
  watch: {
    // 监听表单设置本组件的值
    userId: {
      immediate: true, // 初始创建 watch 时立即执行回调函数
      deep: true, // 监听到对象里面的值的变化
      handler() {
        if (this.userId) {
          this.load()
        }
      },
    }
  },
  computed: {
  }
}
</script>

<style lang="scss">
</style>
